package vip.xiaonuo.smzq.modular.dictionary.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import vip.xiaonuo.common.pojo.CommonResult;
import vip.xiaonuo.smzq.core.gson.GsonUtil;
import vip.xiaonuo.smzq.core.tools.excel.ExcelImportUtil;
import vip.xiaonuo.smzq.modular.dictionary.entity.Cb02Kslx;
import vip.xiaonuo.smzq.modular.dictionary.entity.excelEnt.Cb02KslxExc;
import vip.xiaonuo.smzq.modular.dictionary.service.ICb02KslxService;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;


/**
 * 矿石类型
 *
 * @author eomer
 * @since 2024-02-21
 */
@Api(tags = "矿石类型")
@RestController
@RequestMapping("/dictionary/cb02Kslx")
@Validated
public class Cb02KslxController {

    @Resource
    private ICb02KslxService iCb02KslxService;

    /**
     * 添加矿石类型
     */
    @ApiOperation("添加矿石类型")
    @PostMapping("/add")
    public CommonResult<String> add(@RequestBody @Valid Cb02Kslx cb02Kslx) {
        iCb02KslxService.save(cb02Kslx);
        return CommonResult.ok();
    }

    /**
     * 删除矿石类型
     */
    @ApiOperation("删除矿石类型")
    @PostMapping("/delete")
    public CommonResult<String> delete(@ApiParam(name = "pkid", value = "主键") @RequestParam String pkid) {
        iCb02KslxService.removeById(pkid);
        return CommonResult.ok();
    }

    /**
     * 编辑矿石类型
     */
    @ApiOperation("编辑矿石类型")
    @PostMapping("/edit")
    public CommonResult<String> edit(@RequestBody @Valid Cb02Kslx cb02Kslx) {
        iCb02KslxService.updateById(cb02Kslx);
        return CommonResult.ok();
    }

    /**
     * 矿石类型分页查询
     */
    @ApiOperation("矿石类型分页查询")
    @GetMapping("/list")
    public CommonResult<Page<Cb02Kslx>> page() {
        return CommonResult.data(iCb02KslxService.page(new Cb02Kslx()));
    }

    /**
     * 矿石类型筛选下拉
     * kcdm 矿产代码
     */
    @ApiOperation("矿石类型筛选下拉")
    @GetMapping("/kslxList")
    public CommonResult<List<Cb02Kslx>> kslxList(Integer kcdm) {
        return CommonResult.data(iCb02KslxService.getKslxList(kcdm));
    }

    /**
     * 导入
     */
    @ApiOperation("导入")
    @PostMapping("/import")
    public CommonResult<String> importExcel(@RequestParam("file") MultipartFile file) {
        try {
            List<Cb02KslxExc> list = ExcelImportUtil.readMultipartFile(file, Cb02KslxExc.class);
            List<Cb02Kslx> cb01Zhs = GsonUtil.ObjectToList(list, Cb02Kslx.class);
            iCb02KslxService.saveBatch(cb01Zhs);
            return CommonResult.ok();
        } catch (Exception e) {
            return CommonResult.error(e.getMessage());
        }
    }
}
